package logic

import (
	"anban/models"
	"anban/service"
	"anban/utils"
	"anban/utils/wechat"
	"fmt"
	"github.com/astaxie/beego"
	"time"
)

// 发送体温通知
func SendTempNotice(student *models.Student, first, temp, remark string) {
	domain := beego.AppConfig.String("domain")
	path := beego.URLFor("parent.TempNoticeController.ShowList")
	wechatUrl := fmt.Sprintf("%s%s", domain, path)
	familySn := student.FamilySn

	if len(familySn) > 0 {
		p := map[string]interface{}{}
		p["familySn"] = familySn
		p["status"] = 1
		p["subscribeStatus"] = 1
		_, userList := service.GetUserList(p)
		for _, user := range userList {
			wechat.SendTempNotice(user.OpenId, first, remark, wechatUrl, student.RealName, time.Now().Format("2006-01-02 15:04"), temp)
		}
	}
}

// 发送体温异常通知
func SendTempAbnormalNotice(student *models.Student, first, temp, remark string) {
	domain := beego.AppConfig.String("domain")
	path := beego.URLFor("parent.TempNoticeController.ShowList")
	wechatUrl := fmt.Sprintf("%s%s", domain, path)
	familySn := student.FamilySn
	tplId := beego.AppConfig.String("tempnotnormaltplid")

	if len(familySn) > 0 {
		p := map[string]interface{}{}
		p["familySn"] = familySn
		p["status"] = 1
		_, userList := service.GetUserList(p)
		for _, user := range userList {
			if user.SubscribeStatus == 1 {
				wechat.SendTempNotice(user.OpenId, first, remark, wechatUrl, student.RealName, time.Now().Format("2006-01-02 15:04"), temp)
			}
			if len(user.Phone) > 1 {
				utils.SendMobileMessage([]string{user.Phone}, tplId, []string{user.RealName})
			}
		}
	}
}
